import { http } from '../utils/http/index'
import type { UploadSignResponse } from '../models/Upload'

// 公司信息接口
export interface CompanyInfo {
  id: string
  companyName: string
  shortName: string
  logo: string
  address: string
  nature: string
  peopleSize: string
  industry: string
  isVip: number
  vipExpireDate: string
  hrCounts: number
}

/**
 * 根据公司全称获取公司信息
 */
export async function getCompanyByFullName(fullName: string): Promise<CompanyInfo | null> {
  try {
    const response = await http.get(`/portal/company/get_by_full_name?fullName=${encodeURIComponent(fullName)}`)
    return response as CompanyInfo
  } catch (error) {
    return null
  }
}

/**
 * 获取公司申请上传签名
 */
export async function getCompanyApplyUploadSign(): Promise<UploadSignResponse> {
  const response = await http.get('/portal/company/get_apply_upload_sign')
  return response as UploadSignResponse
}

// 公司申请请求体
export interface CompanyApplyRequest {
  companyName: string
  shortName: string
  logo: string
  bizLicense: string
  industry: string
  peopleSize: string
  applicantRealName: string
  applicantPosition: string
}

/**
 * 提交公司申请
 */
export async function companyApply(data: CompanyApplyRequest): Promise<void> {
  await http.post('/portal/company/apply', data)
}

/**
 * 扫码获取preToken
 */
export async function scanCodeLogin(qrToken: string): Promise<string> {
  const response = await http.get(`/portal/company/scanCode?qrToken=${qrToken}`)
  return response as string
}

/**
 * 确认登录请求体
 */
export interface ConfirmLoginRequest {
  preToken: string
  qrToken: string
}

/**
 * 确认在手机端进行登录
 */
export async function confirmLogin(data: ConfirmLoginRequest): Promise<void> {
  await http.post('/portal/company/confirmLogin', data)
}

/**
 * 企业详情信息
 */
export interface CompanyDetail {
  id: string
  companyName: string
  shortName: string
  logo: string
  bizLicense: string
  country: string | null
  province: string | null
  city: string | null
  district: string | null
  address: string
  nature: string
  peopleSize: string
  industry: string
  financStage: string
  workTime: string
  introduction: string
  advantage: string
  benefits: string
  bonus: string
  subsidy: string
  buildDate: string
  registCapital: string
  registPlace: string
  legalRepresentative: string
  authLetter: string | null
  isVip: number
  vipExpireDate: string
  photos: string
}

/**
 * 获取公司详情
 * @param companyId 可选，如果提供则根据ID查询，否则查询当前用户绑定的公司
 */
export async function getCompanyDetail(companyId?: string): Promise<CompanyDetail> {
  let url: string
  if (companyId) {
    url = `/portal/company/detail?companyId=${companyId}`
  } else {
    url = '/portal/user/my_bind_company/detail'
  }
  const response = await http.get(url)
  return response as CompanyDetail
}

/**
 * 企业基本信息修改请求体
 */
export interface CompanyBasicInfoModifyRequest {
  companyId: string
  shortName: string
  logo: string
  country: string
  province: string
  city: string
  district: string
  address: string
  nature: string
  peopleSize: string
  industry: string
  financStage: string
  introduction: string
  buildDate: string
  registCapital: string
  registPlace: string
  legalRepresentative: string
}

/**
 * 修改企业基本信息
 */
export async function modifyCompanyBasicInfo(data: CompanyBasicInfoModifyRequest): Promise<void> {
  await http.put('/portal/company/info/modify', data)
}

/**
 * 获取企业信息编辑上传签名
 */
export async function getCompanyInfoUploadSign(): Promise<UploadSignResponse> {
  const response = await http.get('/portal/company/get_apply_upload_sign')
  return response as UploadSignResponse
}

/**
 * 批量获取公司相册上传签名
 */
export async function getCompanyPhotoUploadSigns(count: number): Promise<UploadSignResponse[]> {
  const response = await http.get(`/portal/company/get_company_photo_upload_signs?count=${count}`)
  return response as UploadSignResponse[]
}

/**
 * 更新公司相册请求体
 */
export interface UpdateCompanyPhotosRequest {
  companyId: string
  photos: string
}

/**
 * 更新公司相册
 */
export async function updateCompanyPhotos(data: UpdateCompanyPhotosRequest): Promise<void> {
  await http.post('/portal/company/update_company_photos', data)
}

/**
 * 更新公司工作时间请求体
 */
export interface UpdateCompanyWorkTimeRequest {
  companyId: string
  workTime: string
}

/**
 * 更新公司工作时间
 */
export async function updateCompanyWorkTime(data: UpdateCompanyWorkTimeRequest): Promise<void> {
  await http.put('/portal/company/info/modify', data)
}

/**
 * 更新公司优势请求体
 */
export interface UpdateCompanyAdvantageRequest {
  companyId: string
  advantage: string     // 公司优势
  benefits: string      // 福利待遇
  bonus: string         // 薪资福利
  subsidy: string       // 补助津贴
}

/**
 * 更新公司优势
 */
export async function updateCompanyAdvantage(data: UpdateCompanyAdvantageRequest): Promise<void> {
  await http.put('/portal/company/info/modify', data)
}
